package com.winz.cultureapp.widge;

import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.winz.cultureapp.R;

import java.util.HashMap;
import java.util.Map;


/**
 * 修改密码
 */
public class ChangPwdDialog {

    private AlertDialog dialog;
    private final Button btn_ok;
    private final EditText et_old;
    private final EditText et_new;
    private final EditText et_again;
    private OkButtonOnClickListener listener;

    private Map<String,String> data = new HashMap<>();

    public ChangPwdDialog(Context context){
        dialog = new AlertDialog.Builder(context).create();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setView(LayoutInflater.from(context).inflate(R.layout.dialog_change_pwd,null));
        dialog.show();

//        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
//        lp.width = (AppUtils.getScreenWidth((Activity) context)); //设置宽度
//        dialog.getWindow().setAttributes(lp);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); // 非常重要：设置对话框弹出的位置
        window.setContentView(R.layout.dialog_change_pwd);

        et_old = (EditText) window.findViewById(R.id.et_old);
        et_new = (EditText) window.findViewById(R.id.et_new);
        et_again = (EditText) window.findViewById(R.id.et_again);
        Button btn_no = (Button) window.findViewById(R.id.btn_no);
        btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        btn_ok = (Button) window.findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.put("old",et_old.getText().toString());
                data.put("new",et_new.getText().toString());
                data.put("again",et_again.getText().toString());
                if (listener != null){
                    listener.onFinished(data);
                }
            }
        });

    }



    /**
     * 关闭对话框
     */
    public void dismiss() {

        dialog.dismiss();
    }

    public void setOkButtonOnClickListener(OkButtonOnClickListener listener) {
        this.listener = listener;
    }

    public interface OkButtonOnClickListener{
        void onFinished(Map<String,String> data);
    }

    public void setMsg(Map<String,String> data){
        et_old.setText(data.get("old"));
        et_new.setText(data.get("new"));
        et_again.setText(data.get("again"));
    }
    public Map<String,String> getData(){
        return data;
    }

    public void setBtn_okText(String ok){
        btn_ok.setText(ok);
    }

//    public void showKeyboard() {
//        if(et_old!=null){
//            //设置可获得焦点
//            et_old.setFocusable(true);
//            et_old.setFocusableInTouchMode(true);
//            //请求获得焦点
//            et_old.requestFocus();
//            //调用系统输入法
//            InputMethodManager inputManager = (InputMethodManager) et_old.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//            inputManager.showSoftInput(et_old, 0);
//        }
//    }
}
